import { Component } from '@angular/core';
import { NavController, Loading,LoadingController, App } from 'ionic-angular';
import { HttpClient,HttpHeaders } from '@angular/common/http'
import { NativeStorage } from '@ionic-native/native-storage';
import { TranslateService } from '@ngx-translate/core';

import { ProductrentPage } from "../product/rent/rent";
import { ProductservicePage } from "../product/service/service";
import { ProductbuyPage } from "../product/buy/buy";
import { ProductperPage } from "../product/per/per";
import { AppConfig } from '../../app/app.config';
@Component({
  selector: 'page-contact',
  templateUrl: 'favorites.html'
})
export class FavoritesPage {
  list:any
  loading:any
  src = AppConfig.getMainUrl();
  constructor(
    private app:App,
    private navCtrl: NavController,
    private http:HttpClient,
    private loadingCtrl:LoadingController,
    private stroge:NativeStorage,
    private translate: TranslateService
  ) {
    translate.setDefaultLang('en')

  }
  loadWait(){
      this.loading = this.loadingCtrl.create({
          content: 'Please wait...'
      });
      this.loading.present();
  }
  ionViewWillEnter(){
    this.stroge.getItem('lang').then(res=>{
      this.translate.use(res)
    })
    this.stroge.getItem('token').then(token=>{
      this.loadWait()
      this.http.get(AppConfig.getUrl()+'house/getMyCollections?token='+token).subscribe(res=>{
          if(res['code']==101){
              this.list = res['data']['rows']
              this.datatodata()
          }
          this.loading.dismiss()
      })
    })
  }
  goDetail(item:any){
    let page
    switch(item.cate){
      case 1:
        page = ProductrentPage
        break
      case 2:
        page = ProductbuyPage
        break
      case 3:
        page = ProductperPage
        break
      case 4:
        page = ProductservicePage
        break
    }
    this.app.getRootNav().push(page,{id:item.id})
  }
  datatodata(){
    this.list.forEach((item,i) => {
      switch(item.cate){
        case 1:
          this.list[i].cateName='Rent'
          break
        case 2:
          this.list[i].cateName='Buy'
          break
        case 3:
          this.list[i].cateName='Rent per day'
          break
        case 4:
          this.list[i].cateName='Service'
          break
      }
      switch(item.price_unit){
        case 1:
          this.list[i].price_unit='£'
          break
        case 2:
          this.list[i].price_unit='€'
          break
        case 3:
          this.list[i].price_unit='$'
          break
        case 4:
          this.list[i].price_unit='¥'
          break
      }
      switch(item.price_type){
        case 1:
          this.list[i].price_type='Month'
          break
        case 2:
          this.list[i].price_type='Week'
          break
        case 3:
          this.list[i].price_type='Day'
          break
      }
      switch(item.area_unit){
        case 1:
          this.list[i].area_unit='SQ.FT'
          this.list[i].other_area = Math.ceil(this.list[i].area / 10.764);
          this.list[i].other_area_unit='SQ.M'
          break
        case 2:
          this.list[i].area_unit='SQ.M'
          this.list[i].other_area = Math.ceil(this.list[i].area * 10.764);
          this.list[i].other_area_unit='SQ.FT'
          break
      }
      this.list[i].img = 'url('+AppConfig.getMainUrl()+item.logo+')';
    });
  }
  
}
